/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.Type;

import com.cea.papyrus.core.listener.UmlElementListener;
import com.cea.papyrus.core.ui.dialogs.AssociationTreeSelectionDialog;
import com.cea.papyrus.core.utils.LookForElement;

/**
 * Section that displays the type of a connector(association)
 */
public class TypeConnectorSection extends TypeSection {

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.TypeSection#refresh()
	 */
	@Override
	public void refresh(){
		if(!browseButton.isDisposed()){
			Type type = null;
			if (getElement() instanceof Connector) {
				type = ((Connector)getElement()).getType() ;
			}

			if(type == null){
				if(!label.isDisposed())
					label.setText("<UNDEFINED>");
				if(!removeButton.isDisposed())
					removeButton.setEnabled(false);
			} else {
				if(!label.isDisposed())
					label.setText(type.getQualifiedName());
				if(!removeButton.isDisposed())
					removeButton.setEnabled(true);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.TypeSection#removeButtonPressed()
	 */
	@Override
	protected void removeButtonPressed(){
		if(getElement() instanceof Connector) {
			((Connector)getElement()).setType(null);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.TypeSection#browseButtonPressed()
	 */
	@Override
	protected void browseButtonPressed(){
		final int[] result = {IDialogConstants.CANCEL_ID};
		final AssociationTreeSelectionDialog dialog = new AssociationTreeSelectionDialog(parent.getShell(), LookForElement.topPackage(getElement()));

		// open dialog window
		BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
			public void run() {
				result[0] = dialog.open();
			}
		});

		// if ok was pressed, and a type was selected, set new type
		if(IDialogConstants.OK_ID == result[0]){
			Association type = dialog.getResult(); 
			if(type!= null){
				
				// remove previous listener, if there was one before
				((Connector)getElement()).setType(type);

				// add a listener
				if( graphicalElement!=null){
					UmlElementListener _adapter=new UmlElementListener(graphicalElement);
					((Notifier)type).eAdapters().add(_adapter);}
			}
		}
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.TypeSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if((removeButton != null) && !removeButton.isDisposed())
			removeButton.removeMouseListener(removeButtonListener);
		if((browseButton != null) && !browseButton.isDisposed())
			browseButton.removeMouseListener(browseButtonListener);
	}
}